#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/07/24 14:02
# @Author : zengxiaolong
# @File    : sd_strategy.py
import requests


class TestSdStrategy(object):
    def __init__(self):
        self.url = r'http://api-dw.zhcxkj.com/dw/sales/GetAmazonProductBasesByScrollId_1'
        self.headers = {
            'Accept': r'*/*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Authorization': r'Basic aXJvYm90Ym94OmkxcjVvOEJvdGJveA==',
            'Content-Type': r'application/json'
        }
        self.data = None
        self.product_data = []

    def get_amazon_product(self, datas):
        self.product_data = []
        self.data = datas
        response = requests.request(method='POST', url=self.url, json=self.data, headers=self.headers)
        # 获取满足筛选条件的产品订单来源渠道，渠道sku，asin
        product_list = response.json()['data']['list']
        # print('满足条件的产品：{}'.format(product_list))
        if product_list:
            for p in product_list:
                self.product_data.append({'OrderSourceId': p['OrderSourceId'], 'OrderSourceName': p['OrderSourceName'],
                                          'OrderSourceSKU': p['OrderSourceSKU'], 'ASINorItemId': p['ASINorItemId'],
                                          'SKU': p['SKU'],
                                          'NewClassId': p['NewClassId'],
                                          'NewClassId2': p['NewClassId2'], 'NewClassId3': p['NewClassId3'],
                                          'FBA可用库存': p.get('KykcNum', 0),
                                          'FBA采购中数量': p.get('cgzNum', 0), '运输中数量': p.get('ystzNum', 0),
                                          '估算销量': p['AvgDailySales'], 'Listing的售价': p['ListingPrice'],
                                          '上架人员Id': p['AddAdminId'],
                                          '销售人员ID': p['BusinessAdminId']})
        else:
            print('没有产品满足筛选条件')

    def get_fba_inventory(self, d):
        self.get_amazon_product(d)
        for i, v in enumerate(self.product_data):
            order_source_id = v['OrderSourceId']
            form = {
                'customerId': 1,
                'orderSourceId': order_source_id,
                'reportType': 'GET_FBA_INVENTORY_PLANNING_DATA',
                'date': '2023-07-25T00:00:00+08:00'
            }
            headers = {
                'Authorization': 'Basic YXBpdXNlcjppMXI1bzhCb3Rib3g=',

            }
            res = requests.request(method='GET', url=r'https://dataservice-hd.zhcxkj.com/amazon/reports/latest',
                                   headers=headers,
                                   params=form)
            # print(res.json()['data']['list'])
            if res.json()['data']['list']:
                # print(res.json()['data']['list'])
                for _ in res.json()['data']['list']:
                    if _['asin'] == v['ASINorItemId']:
                        # print("---------------------------", _['inv_age_0_to_90_days'])
                        self.product_data[i]['inv_age_0_to_90_days'] = _['inv_age_0_to_90_days']
                        self.product_data[i]['inv_age_91_to_180_days'] = _['inv_age_91_to_180_days']
                        self.product_data[i]['inv_age_181_to_270_days'] = _['inv_age_181_to_270_days']
                        self.product_data[i]['inv_age_271_to_365_days'] = _['inv_age_271_to_365_days']
                        self.product_data[i]['inv_age_365_plus_days'] = _['inv_age_365_plus_days']
            else:
                print('未查到任何产品在FBA库存的库龄信息')

    def test_query_01(self):
        datas = {
            "size": 10000,
            "input": {
                "publishTime": ["2023-07-20T00:00:00.2289878+08:00", "2023-07-24 23:59:59"],
                "customerId": 1,
                "orderSourceIds": [
                    6082
                ],
                "onShelfModes": [
                    0, 1, 3,
                ],
                "isFBA": False,
                "stopProducts": [
                    0
                ],
                "productPropertys": [
                    1
                ],
                "isNullholidaysId": True,
            }
        }
        self.get_amazon_product(datas)

        data = []
        print('=' * 200)
        for _ in self.product_data:
            if _['OrderSourceSKU'] and _['SKU'] and _['ASINorItemId']:
                data.append(_)
                print(_)
        print("一共匹配到{}个满足条件的产品".format(len(data)))

    def test_query_02(self):
        datas = {
            "size": 10000,
            "input": {
                "publishTime": ["2023-07-20T00:00:00.2289878+08:00", "2023-07-24 23:59:59"],
                "customerId": 1,
                "orderSourceIds": [
                    6082
                ],
                "onShelfModes": [
                    0, 1, 2, 3, 4, 5, 6, 7, 8, 9
                ],
                "isFBA": True,
                "stopProducts": [
                    0, 1, 3
                ],
                "productPropertys": [
                    0, 1
                ],
                "isNullholidaysId": True,
                "reviewStars": [0, 5.0]

            }
        }
        self.get_fba_inventory(datas)
        print('=========================================================================================')
        print("一共匹配到{}个满足条件的产品".format(len(self.product_data)))
        for _ in self.product_data:
            print(_)

    def test_query_03(self):
        datas = {
            "size": 10000,
            "input": {
                "publishTime": ["2023-07-20T00:00:00.2289878+08:00", "2023-07-24 23:59:59"],
                "customerId": 1,
                "orderSourceIds": [
                    6082, 6101
                ],
                "onShelfModes": [
                    0, 1, 2, 3, 4, 5, 6, 7
                ],
                "isFBA": False,
                "stopProducts": [
                    0, 1
                ],
                "productPropertys": [
                    0, 1
                ],
                "isNullholidaysId": True,

            }
        }
        self.get_fba_inventory(datas)
        print('=========================================================================================')
        print("一共匹配到{}个满足条件的产品".format(len(self.product_data)))
        for _ in self.product_data:
            print(_)

    def test_query_04(self):
        datas = {
            "size": 10000,
            "input": {
                "publishTime": ["2023-07-20T00:00:00.2289878+08:00", "2023-07-24 23:59:59"],
                "customerId": 1,
                "orderSourceIds": [
                    6082
                ],
                "onShelfModes": [
                    0, 1, 2, 3, 4, 5, 6, 7, 8, 9
                ],
                "isFBA": False,
                "stopProducts": [
                    0, 1
                ],
                "productPropertys": [
                    1
                ],
                "isNullholidaysId": True,

            }
        }
        self.get_fba_inventory(datas)
        print('=========================================================================================')
        print("一共匹配到{}个满足条件的产品".format(len(self.product_data)))
        for _ in self.product_data:
            print(_)


if __name__ == '__main__':
    t = TestSdStrategy()
    t.test_query_01()
